package com.esri.core.geometry;

import com.github.mikephil.charting.utils.Utils;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/* loaded from: classes.dex */
class OperatorExportToWKBCursor extends ByteBufferCursor {
    GeometryCursor a;
    int b;
    int c = -1;
    ByteBuffer d;

    public OperatorExportToWKBCursor(int i, GeometryCursor geometryCursor) {
        if (geometryCursor == null) {
            throw new IllegalArgumentException();
        }
        this.b = i;
        this.a = geometryCursor;
        this.d = null;
    }

    @Override // com.esri.core.geometry.ByteBufferCursor
    public int getByteBufferID() {
        return this.c;
    }

    @Override // com.esri.core.geometry.ByteBufferCursor
    public ByteBuffer next() {
        Geometry next = this.a.next();
        if (next != null) {
            this.c = this.a.getGeometryID();
            int a = a(this.b, next, (ByteBuffer) null);
            ByteBuffer byteBuffer = this.d;
            if (byteBuffer == null || a > byteBuffer.capacity()) {
                this.d = ByteBuffer.allocate(a).order(ByteOrder.nativeOrder());
            }
            a(this.b, next, this.d);
            return this.d;
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static int a(int i, Geometry geometry, ByteBuffer byteBuffer) {
        if (geometry == null) {
            return 0;
        }
        int value = geometry.getType().value();
        if (value == 513) {
            if ((i & 4) != 0 || (i & 8) != 0 || (i & 16) != 0 || (i & 32) != 0) {
                throw new GeometryException("invalid argument");
            }
            return a(i, (Point) geometry, byteBuffer);
        } else if (value == 3077) {
            if ((i & 4) != 0 || (i & 8) != 0 || (i & 1) != 0 || (i & 2) != 0) {
                throw new GeometryException("invalid argument");
            }
            return a(i, (Envelope) geometry, byteBuffer);
        } else if (value == 8710) {
            if ((i & 4) != 0 || (i & 8) != 0 || (i & 16) != 0 || (i & 32) != 0) {
                throw new GeometryException("invalid argument");
            }
            return a(i, (MultiPoint) geometry, byteBuffer);
        } else if (value == 25607) {
            if ((i & 16) != 0 || (i & 32) != 0 || (i & 1) != 0 || (i & 2) != 0) {
                throw new GeometryException("invalid argument");
            }
            return a(i, (Polyline) geometry, byteBuffer);
        } else if (value == 27656) {
            if ((i & 4) != 0 || (i & 8) != 0 || (i & 1) != 0 || (i & 2) != 0) {
                throw new GeometryException("invalid argument");
            }
            return a(i, (Polygon) geometry, byteBuffer);
        } else {
            throw new GeometryException("internal error");
        }
    }

    private static int a(int i, Envelope envelope, ByteBuffer byteBuffer) {
        int i2;
        boolean z = envelope.e(1) && (i & 64) == 0;
        boolean z2 = envelope.e(2) && (i & 128) == 0;
        boolean isEmpty = envelope.isEmpty();
        int i3 = !isEmpty;
        int i4 = isEmpty != 0 ? 0 : 5;
        int i5 = i & 32;
        int i6 = ((i5 != 0 || i3 == 0) ? 9 : 0) + (i3 * 9) + (i3 * 4) + (i4 * 16);
        if (z) {
            i6 += i4 * 8;
        }
        if (z2) {
            i6 += i4 * 8;
        }
        if (i6 < NumberUtils.intMax()) {
            if (byteBuffer == null) {
                return i6;
            }
            if (byteBuffer.capacity() < i6) {
                throw new GeometryException("buffer is too small");
            }
            byte b = (byte) (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN ? 1 : 0);
            int i7 = 2003;
            if (!z && !z2) {
                if (i5 != 0 || (i3 == 0 && (i & 16) == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 6);
                    byteBuffer.putInt(5, i3);
                } else if (i3 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 3);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 3;
                }
                i2 = 9;
                i7 = 3;
            } else if (z && !z2) {
                if (i5 != 0 || (i3 == 0 && (i & 16) == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1006);
                    byteBuffer.putInt(5, i3);
                } else if (i3 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 1003;
                }
                i2 = 9;
                i7 = 1003;
            } else if (!z2 || z) {
                if (i5 != 0 || (i3 == 0 && (i & 16) == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, WkbGeometryType.wkbMultiPolygonZM);
                    byteBuffer.putInt(5, i3);
                } else if (i3 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 3003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 3003;
                }
                i2 = 9;
                i7 = 3003;
            } else {
                if (i5 != 0 || (i3 == 0 && (i & 16) == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, WkbGeometryType.wkbMultiPolygonM);
                    byteBuffer.putInt(5, i3);
                } else if (i3 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 2003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                }
                i2 = 9;
            }
            if (i3 == 0) {
                return i2;
            }
            byteBuffer.put(i2, b);
            int i8 = i2 + 1;
            byteBuffer.putInt(i8, i7);
            int i9 = i8 + 4;
            byteBuffer.putInt(i9, 1);
            int i10 = i9 + 4;
            byteBuffer.putInt(i10, 5);
            int i11 = i10 + 4;
            Envelope2D envelope2D = new Envelope2D();
            envelope.queryEnvelope2D(envelope2D);
            Envelope1D queryInterval = envelope.queryInterval(1, 0);
            Envelope1D queryInterval2 = z2 ? envelope.queryInterval(2, 0) : null;
            byteBuffer.putDouble(i11, envelope2D.xmin);
            int i12 = i11 + 8;
            byteBuffer.putDouble(i12, envelope2D.ymin);
            int i13 = i12 + 8;
            if (z) {
                byteBuffer.putDouble(i13, queryInterval.vmin);
                i13 += 8;
            }
            if (z2) {
                byteBuffer.putDouble(i13, queryInterval2.vmin);
                i13 += 8;
            }
            byteBuffer.putDouble(i13, envelope2D.xmax);
            int i14 = i13 + 8;
            byteBuffer.putDouble(i14, envelope2D.ymin);
            int i15 = i14 + 8;
            if (z) {
                byteBuffer.putDouble(i15, queryInterval.vmax);
                i15 += 8;
            }
            if (z2) {
                byteBuffer.putDouble(i15, queryInterval2.vmax);
                i15 += 8;
            }
            byteBuffer.putDouble(i15, envelope2D.xmax);
            int i16 = i15 + 8;
            byteBuffer.putDouble(i16, envelope2D.ymax);
            int i17 = i16 + 8;
            if (z) {
                byteBuffer.putDouble(i17, queryInterval.vmin);
                i17 += 8;
            }
            if (z2) {
                byteBuffer.putDouble(i17, queryInterval2.vmin);
                i17 += 8;
            }
            byteBuffer.putDouble(i17, envelope2D.xmin);
            int i18 = i17 + 8;
            byteBuffer.putDouble(i18, envelope2D.ymax);
            int i19 = i18 + 8;
            if (z) {
                byteBuffer.putDouble(i19, queryInterval.vmax);
                i19 += 8;
            }
            if (z2) {
                byteBuffer.putDouble(i19, queryInterval2.vmax);
                i19 += 8;
            }
            byteBuffer.putDouble(i19, envelope2D.xmin);
            int i20 = i19 + 8;
            byteBuffer.putDouble(i20, envelope2D.ymin);
            int i21 = i20 + 8;
            if (z) {
                byteBuffer.putDouble(i21, queryInterval.vmin);
                i21 += 8;
            }
            if (z2) {
                byteBuffer.putDouble(i21, queryInterval2.vmin);
                return i21 + 8;
            }
            return i21;
        }
        throw new GeometryException("invalid call");
    }

    private static int a(int i, Point point, ByteBuffer byteBuffer) {
        int i2;
        boolean z;
        int i3;
        int i4 = 0;
        boolean z2 = point.e(1) && (i & 64) == 0;
        boolean z3 = point.e(2) && (i & 128) == 0;
        int i5 = !point.isEmpty();
        int i6 = i & 2;
        if (i6 != 0) {
            i2 = (i5 * 21) + 9;
            if (z2) {
                i2 += i5 * 8;
            }
            if (z3) {
                i2 += i5 * 8;
            }
        } else {
            i2 = z2 ? 29 : 21;
            if (z3) {
                i2 += 8;
            }
        }
        if (i2 < NumberUtils.intMax()) {
            if (byteBuffer == null) {
                return i2;
            }
            if (byteBuffer.capacity() < i2) {
                throw new GeometryException("buffer is too small");
            }
            byte b = (byte) (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN ? 1 : 0);
            if (!z2 && !z3) {
                if (i6 != 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 4);
                    byteBuffer.putInt(5, i5);
                    z = z2;
                    i4 = 9;
                } else if (i5 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1);
                    byteBuffer.putDouble(5, NumberUtils.NaN());
                    byteBuffer.putDouble(13, NumberUtils.NaN());
                    z = z2;
                    i4 = 21;
                } else {
                    z = z2;
                }
                i3 = 1;
            } else if (!z2 || z3) {
                z = z2;
                if (!z3 || z) {
                    i3 = 3001;
                    if (i6 != 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, WkbGeometryType.wkbMultiPointZM);
                        byteBuffer.putInt(5, i5);
                        i4 = 9;
                    } else if (i5 == 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, 3001);
                        byteBuffer.putDouble(5, NumberUtils.NaN());
                        byteBuffer.putDouble(13, NumberUtils.NaN());
                        byteBuffer.putDouble(21, NumberUtils.NaN());
                        byteBuffer.putDouble(29, NumberUtils.NaN());
                        i4 = 37;
                    }
                } else {
                    i3 = 2001;
                    if (i6 != 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, WkbGeometryType.wkbMultiPointM);
                        byteBuffer.putInt(5, i5);
                        i4 = 9;
                    } else if (i5 == 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, 2001);
                        byteBuffer.putDouble(5, NumberUtils.NaN());
                        byteBuffer.putDouble(13, NumberUtils.NaN());
                        byteBuffer.putDouble(21, NumberUtils.NaN());
                        i4 = 29;
                    }
                }
            } else {
                i3 = 1001;
                if (i6 != 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1004);
                    byteBuffer.putInt(5, i5);
                    z = z2;
                    i4 = 9;
                } else if (i5 == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1001);
                    z = z2;
                    byteBuffer.putDouble(5, NumberUtils.NaN());
                    byteBuffer.putDouble(13, NumberUtils.NaN());
                    byteBuffer.putDouble(21, NumberUtils.NaN());
                    i4 = 29;
                } else {
                    z = z2;
                }
            }
            if (i5 == 0) {
                return i4;
            }
            byteBuffer.put(i4, b);
            int i7 = i4 + 1;
            byteBuffer.putInt(i7, i3);
            int i8 = i7 + 4;
            double x = point.getX();
            double y = point.getY();
            byteBuffer.putDouble(i8, x);
            int i9 = i8 + 8;
            byteBuffer.putDouble(i9, y);
            int i10 = i9 + 8;
            if (z) {
                byteBuffer.putDouble(i10, point.getZ());
                i10 += 8;
            }
            if (z3) {
                byteBuffer.putDouble(i10, point.getM());
                return i10 + 8;
            }
            return i10;
        }
        throw new GeometryException("invalid call");
    }

    private static int a(int i, MultiPoint multiPoint, ByteBuffer byteBuffer) {
        int i2;
        boolean z;
        int i3;
        int i4;
        double defaultValue;
        double defaultValue2;
        MultiPointImpl multiPointImpl = (MultiPointImpl) multiPoint._getImpl();
        int i5 = 1;
        int i6 = 0;
        boolean z2 = multiPointImpl.e(1) && (i & 64) == 0;
        boolean z3 = multiPointImpl.e(2) && (i & 128) == 0;
        int pointCount = multiPointImpl.getPointCount();
        int i7 = i & 1;
        if (i7 != 0 && pointCount > 1) {
            throw new GeometryException("invalid argument");
        }
        if (i7 == 0) {
            i2 = (pointCount * 21) + 9;
            if (z2) {
                i2 += pointCount * 8;
            }
            if (z3) {
                i2 += pointCount * 8;
            }
        } else {
            i2 = z2 ? 29 : 21;
            if (z3) {
                i2 += 8;
            }
        }
        if (i2 < NumberUtils.intMax()) {
            if (byteBuffer == null) {
                return i2;
            }
            if (byteBuffer.capacity() < i2) {
                throw new GeometryException("buffer is too small");
            }
            byte b = (byte) (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN ? 1 : 0);
            if (z2 || z3) {
                if (!z2 || z3) {
                    if (!z3 || z2) {
                        z = z3;
                        i3 = 3001;
                        if (i7 == 0) {
                            byteBuffer.put(0, b);
                            byteBuffer.putInt(1, WkbGeometryType.wkbMultiPointZM);
                            byteBuffer.putInt(5, pointCount);
                            i4 = 9;
                        } else if (pointCount == 0) {
                            byteBuffer.put(0, b);
                            byteBuffer.putInt(1, 3001);
                            byteBuffer.putDouble(5, NumberUtils.NaN());
                            byteBuffer.putDouble(13, NumberUtils.NaN());
                            byteBuffer.putDouble(21, NumberUtils.NaN());
                            byteBuffer.putDouble(29, NumberUtils.NaN());
                            i4 = 37;
                        }
                    } else {
                        i3 = 2001;
                        if (pointCount == 0) {
                            byteBuffer.put(0, b);
                            byteBuffer.putInt(1, 2001);
                            z = z3;
                            byteBuffer.putDouble(5, NumberUtils.NaN());
                            byteBuffer.putDouble(13, NumberUtils.NaN());
                            byteBuffer.putDouble(21, NumberUtils.NaN());
                            i4 = 29;
                        } else {
                            z = z3;
                        }
                    }
                    i4 = 0;
                } else {
                    i3 = 1001;
                    if (i7 == 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, 1004);
                        byteBuffer.putInt(5, pointCount);
                        z = z3;
                        i4 = 9;
                    } else if (pointCount == 0) {
                        byteBuffer.put(0, b);
                        byteBuffer.putInt(1, 1001);
                        byteBuffer.putDouble(5, NumberUtils.NaN());
                        byteBuffer.putDouble(13, NumberUtils.NaN());
                        byteBuffer.putDouble(21, NumberUtils.NaN());
                        z = z3;
                        i3 = 1001;
                        i4 = 29;
                    } else {
                        z = z3;
                        i3 = 1001;
                        i4 = 0;
                    }
                }
            } else if (i7 == 0) {
                byteBuffer.put(0, b);
                byteBuffer.putInt(1, 4);
                byteBuffer.putInt(5, pointCount);
                z = z3;
                i3 = 1;
                i4 = 9;
            } else if (pointCount == 0) {
                byteBuffer.put(0, b);
                byteBuffer.putInt(1, 1);
                byteBuffer.putDouble(5, NumberUtils.NaN());
                byteBuffer.putDouble(13, NumberUtils.NaN());
                z = z3;
                i3 = 1;
                i4 = 21;
            } else {
                z = z3;
                i3 = 1;
                i4 = 0;
            }
            if (pointCount == 0) {
                return i4;
            }
            AttributeStreamOfDbl attributeStreamOfDbl = (AttributeStreamOfDbl) multiPointImpl.getAttributeStreamRef(0);
            AttributeStreamOfDbl attributeStreamOfDbl2 = null;
            AttributeStreamOfDbl attributeStreamOfDbl3 = (z2 && multiPointImpl._attributeStreamIsAllocated(1)) ? (AttributeStreamOfDbl) multiPointImpl.getAttributeStreamRef(1) : null;
            if (z && multiPointImpl._attributeStreamIsAllocated(2)) {
                attributeStreamOfDbl2 = (AttributeStreamOfDbl) multiPointImpl.getAttributeStreamRef(2);
            }
            int i8 = i4;
            while (i6 < pointCount) {
                byteBuffer.put(i8, b);
                int i9 = i8 + 1;
                byteBuffer.putInt(i9, i3);
                int i10 = i9 + 4;
                int i11 = i6 * 2;
                double read = attributeStreamOfDbl.read(i11);
                boolean z4 = z2;
                double read2 = attributeStreamOfDbl.read(i11 + i5);
                byteBuffer.putDouble(i10, read);
                int i12 = i10 + 8;
                byteBuffer.putDouble(i12, read2);
                i8 = i12 + 8;
                if (z4) {
                    if (attributeStreamOfDbl3 != null) {
                        defaultValue2 = attributeStreamOfDbl3.read(i6);
                        i5 = 1;
                    } else {
                        i5 = 1;
                        defaultValue2 = VertexDescription.getDefaultValue(1);
                    }
                    byteBuffer.putDouble(i8, defaultValue2);
                    i8 += 8;
                } else {
                    i5 = 1;
                }
                if (z) {
                    if (attributeStreamOfDbl2 != null) {
                        defaultValue = attributeStreamOfDbl2.read(i6);
                    } else {
                        defaultValue = VertexDescription.getDefaultValue(2);
                    }
                    byteBuffer.putDouble(i8, defaultValue);
                    i8 += 8;
                }
                i6++;
                z2 = z4;
            }
            return i8;
        }
        throw new GeometryException("invalid call");
    }

    private static int a(int i, Polyline polyline, ByteBuffer byteBuffer) {
        int i2;
        int i3;
        double defaultValue;
        double defaultValue2;
        double defaultValue3;
        double defaultValue4;
        MultiPathImpl multiPathImpl = (MultiPathImpl) polyline._getImpl();
        int i4 = 1;
        if ((i & 4096) != 0 && multiPathImpl.getIsSimple(Utils.DOUBLE_EPSILON) < 1) {
            throw new GeometryException("corrupted geometry");
        }
        int i5 = 0;
        boolean z = multiPathImpl.e(1) && (i & 64) == 0;
        boolean z2 = multiPathImpl.e(2) && (i & 128) == 0;
        int pathCount = multiPathImpl.getPathCount();
        int i6 = i & 4;
        if (i6 != 0 && pathCount > 1) {
            throw new GeometryException("invalid argument");
        }
        int pointCount = multiPathImpl.getPointCount();
        for (int i7 = 0; i7 < pathCount; i7++) {
            if (multiPathImpl.isClosedPath(i7)) {
                pointCount++;
            }
        }
        int i8 = i & 8;
        int i9 = 9;
        int i10 = ((i8 != 0 || pathCount > 1 || pathCount == 0) ? 9 : 0) + (pathCount * 9) + (pointCount * 16);
        if (z) {
            i10 += pointCount * 8;
        }
        if (z2) {
            i10 += pointCount * 8;
        }
        if (i10 < NumberUtils.intMax()) {
            if (byteBuffer == null) {
                return i10;
            }
            if (byteBuffer.capacity() < i10) {
                throw new GeometryException("buffer is too small");
            }
            byte b = (byte) (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN ? 1 : 0);
            int i11 = 2002;
            if (!z && !z2) {
                if (i8 != 0 || pathCount > 1 || (pathCount == 0 && i6 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 5);
                    byteBuffer.putInt(5, pathCount);
                } else if (pathCount == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 2);
                    byteBuffer.putInt(5, 0);
                } else {
                    i9 = 0;
                }
                i11 = 2;
            } else if (z && !z2) {
                if (i8 != 0 || pathCount > 1 || (pathCount == 0 && i6 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1005);
                    byteBuffer.putInt(5, pathCount);
                } else if (pathCount == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1002);
                    byteBuffer.putInt(5, 0);
                } else {
                    i9 = 0;
                }
                i11 = 1002;
            } else if (!z2 || z) {
                if (i8 != 0 || pathCount > 1 || (pathCount == 0 && i6 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, WkbGeometryType.wkbMultiLineStringZM);
                    byteBuffer.putInt(5, pathCount);
                } else if (pathCount == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 3002);
                    byteBuffer.putInt(5, 0);
                } else {
                    i9 = 0;
                }
                i11 = 3002;
            } else if (i8 != 0 || pathCount > 1 || (pathCount == 0 && i6 == 0)) {
                byteBuffer.put(0, b);
                byteBuffer.putInt(1, WkbGeometryType.wkbMultiLineStringM);
                byteBuffer.putInt(5, pathCount);
            } else if (pathCount == 0) {
                byteBuffer.put(0, b);
                byteBuffer.putInt(1, 2002);
                byteBuffer.putInt(5, 0);
            } else {
                i9 = 0;
            }
            if (pathCount == 0) {
                return i9;
            }
            AttributeStreamOfDbl attributeStreamOfDbl = (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(0);
            AttributeStreamOfDbl attributeStreamOfDbl2 = null;
            AttributeStreamOfDbl attributeStreamOfDbl3 = (z && multiPathImpl._attributeStreamIsAllocated(1)) ? (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(1) : null;
            if (z2 && multiPathImpl._attributeStreamIsAllocated(2)) {
                attributeStreamOfDbl2 = (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(2);
            }
            int i12 = i9;
            int i13 = 0;
            while (i5 < pathCount) {
                byteBuffer.put(i12, b);
                int i14 = i12 + 1;
                byteBuffer.putInt(i14, i11);
                int i15 = i14 + 4;
                int pathEnd = multiPathImpl.getPathEnd(i5);
                int i16 = pathEnd - i13;
                if (multiPathImpl.isClosedPath(i5)) {
                    i16++;
                }
                byteBuffer.putInt(i15, i16);
                i12 = i15 + 4;
                int i17 = i13;
                while (i17 < pathEnd) {
                    int i18 = pathCount;
                    int i19 = i17 * 2;
                    byte b2 = b;
                    int i20 = i13;
                    double read = attributeStreamOfDbl.read(i19);
                    int i21 = i5;
                    double read2 = attributeStreamOfDbl.read(i19 + i4);
                    byteBuffer.putDouble(i12, read);
                    int i22 = i12 + 8;
                    byteBuffer.putDouble(i22, read2);
                    i12 = i22 + 8;
                    if (z) {
                        if (attributeStreamOfDbl3 != null) {
                            defaultValue4 = attributeStreamOfDbl3.read(i17);
                        } else {
                            defaultValue4 = VertexDescription.getDefaultValue(1);
                        }
                        byteBuffer.putDouble(i12, defaultValue4);
                        i12 += 8;
                    }
                    if (z2) {
                        if (attributeStreamOfDbl2 != null) {
                            defaultValue3 = attributeStreamOfDbl2.read(i17);
                        } else {
                            defaultValue3 = VertexDescription.getDefaultValue(2);
                        }
                        byteBuffer.putDouble(i12, defaultValue3);
                        i12 += 8;
                    }
                    i17++;
                    i13 = i20;
                    pathCount = i18;
                    b = b2;
                    i5 = i21;
                    i4 = 1;
                }
                int i23 = pathCount;
                byte b3 = b;
                int i24 = i13;
                if (multiPathImpl.isClosedPath(i5)) {
                    int i25 = i24 * 2;
                    i2 = i5;
                    double read3 = attributeStreamOfDbl.read(i25);
                    double read4 = attributeStreamOfDbl.read(i25 + 1);
                    byteBuffer.putDouble(i12, read3);
                    int i26 = i12 + 8;
                    byteBuffer.putDouble(i26, read4);
                    i12 = i26 + 8;
                    if (z) {
                        if (attributeStreamOfDbl3 != null) {
                            i3 = i24;
                            defaultValue2 = attributeStreamOfDbl3.read(i3);
                        } else {
                            i3 = i24;
                            defaultValue2 = VertexDescription.getDefaultValue(1);
                        }
                        byteBuffer.putDouble(i12, defaultValue2);
                        i12 += 8;
                    } else {
                        i3 = i24;
                    }
                    if (z2) {
                        if (attributeStreamOfDbl2 != null) {
                            defaultValue = attributeStreamOfDbl2.read(i3);
                        } else {
                            defaultValue = VertexDescription.getDefaultValue(2);
                        }
                        byteBuffer.putDouble(i12, defaultValue);
                        i12 += 8;
                    }
                } else {
                    i2 = i5;
                }
                i5 = i2 + 1;
                i13 = pathEnd;
                pathCount = i23;
                b = b3;
                i4 = 1;
            }
            return i12;
        }
        throw new GeometryException("invalid call");
    }

    private static int a(int i, Polygon polygon, ByteBuffer byteBuffer) {
        int i2;
        double defaultValue;
        double defaultValue2;
        double defaultValue3;
        double defaultValue4;
        MultiPathImpl multiPathImpl = (MultiPathImpl) polygon._getImpl();
        if ((i & 4096) != 0 && multiPathImpl.getIsSimple(Utils.DOUBLE_EPSILON) != 2) {
            throw new GeometryException("corrupted geometry");
        }
        int i3 = 0;
        boolean z = multiPathImpl.e(1) && (i & 64) == 0;
        boolean z2 = multiPathImpl.e(2) && (i & 128) == 0;
        int a = multiPathImpl.a();
        int i4 = i & 16;
        if (i4 != 0 && a > 1) {
            throw new GeometryException("invalid argument");
        }
        int pathCount = multiPathImpl.getPathCount();
        int pointCount = multiPathImpl.getPointCount() + pathCount;
        if (pointCount > 0 && a == 0) {
            throw new GeometryException("corrupted geometry");
        }
        int i5 = i & 32;
        int i6 = ((i5 != 0 || a > 1 || a == 0) ? 9 : 0) + (a * 9) + (pathCount * 4) + (pointCount * 16);
        if (z) {
            i6 += pointCount * 8;
        }
        if (z2) {
            i6 += pointCount * 8;
        }
        if (i6 < NumberUtils.intMax()) {
            if (byteBuffer == null) {
                return i6;
            }
            if (byteBuffer.capacity() < i6) {
                throw new GeometryException("buffer is too small");
            }
            byte b = (byte) (byteBuffer.order() == ByteOrder.LITTLE_ENDIAN ? 1 : 0);
            int i7 = 2003;
            if (!z && !z2) {
                if (i5 != 0 || a > 1 || (a == 0 && i4 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 6);
                    byteBuffer.putInt(5, a);
                } else if (a == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 3);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 3;
                }
                i2 = 9;
                i7 = 3;
            } else if (z && !z2) {
                if (i5 != 0 || a > 1 || (a == 0 && i4 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1006);
                    byteBuffer.putInt(5, a);
                } else if (a == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 1003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 1003;
                }
                i2 = 9;
                i7 = 1003;
            } else if (!z2 || z) {
                if (i5 != 0 || a > 1 || (a == 0 && i4 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, WkbGeometryType.wkbMultiPolygonZM);
                    byteBuffer.putInt(5, a);
                } else if (a == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 3003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                    i7 = 3003;
                }
                i2 = 9;
                i7 = 3003;
            } else {
                if (i5 != 0 || a > 1 || (a == 0 && i4 == 0)) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, WkbGeometryType.wkbMultiPolygonM);
                    byteBuffer.putInt(5, a);
                } else if (a == 0) {
                    byteBuffer.put(0, b);
                    byteBuffer.putInt(1, 2003);
                    byteBuffer.putInt(5, 0);
                } else {
                    i2 = 0;
                }
                i2 = 9;
            }
            if (a == 0) {
                return i2;
            }
            AttributeStreamOfDbl attributeStreamOfDbl = (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(0);
            AttributeStreamOfDbl attributeStreamOfDbl2 = null;
            AttributeStreamOfDbl attributeStreamOfDbl3 = (z && multiPathImpl._attributeStreamIsAllocated(1)) ? (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(1) : null;
            if (z2 && multiPathImpl._attributeStreamIsAllocated(2)) {
                attributeStreamOfDbl2 = (AttributeStreamOfDbl) multiPathImpl.getAttributeStreamRef(2);
            }
            int i8 = 0;
            int i9 = 0;
            while (i3 < a) {
                byteBuffer.put(i2, b);
                int i10 = i2 + 1;
                byteBuffer.putInt(i10, i7);
                int i11 = i10 + 4;
                AttributeStreamOfInt8 pathFlagsStreamRef = multiPathImpl.getPathFlagsStreamRef();
                int i12 = a;
                int i13 = i8 + 1;
                while (i13 < pathCount && (pathFlagsStreamRef.read(i13) & 4) == 0) {
                    i13++;
                }
                byteBuffer.putInt(i11, i13 - i8);
                i2 = i11 + 4;
                while (i8 < i13) {
                    int pathEnd = multiPathImpl.getPathEnd(i8);
                    MultiPathImpl multiPathImpl2 = multiPathImpl;
                    byteBuffer.putInt(i2, (pathEnd - i9) + 1);
                    int i14 = i2 + 4;
                    int i15 = i9 * 2;
                    int i16 = pathCount;
                    byte b2 = b;
                    double read = attributeStreamOfDbl.read(i15);
                    int i17 = i7;
                    int i18 = i8;
                    double read2 = attributeStreamOfDbl.read(i15 + 1);
                    byteBuffer.putDouble(i14, read);
                    int i19 = i14 + 8;
                    byteBuffer.putDouble(i19, read2);
                    i2 = i19 + 8;
                    if (z) {
                        if (attributeStreamOfDbl3 != null) {
                            defaultValue4 = attributeStreamOfDbl3.read(i9);
                        } else {
                            defaultValue4 = VertexDescription.getDefaultValue(1);
                        }
                        byteBuffer.putDouble(i2, defaultValue4);
                        i2 += 8;
                    }
                    if (z2) {
                        if (attributeStreamOfDbl2 != null) {
                            defaultValue3 = attributeStreamOfDbl2.read(i9);
                        } else {
                            defaultValue3 = VertexDescription.getDefaultValue(2);
                        }
                        byteBuffer.putDouble(i2, defaultValue3);
                        i2 += 8;
                    }
                    int i20 = pathEnd - 1;
                    while (i20 >= i9) {
                        int i21 = i20 * 2;
                        double read3 = attributeStreamOfDbl.read(i21);
                        int i22 = i9;
                        double read4 = attributeStreamOfDbl.read(i21 + 1);
                        byteBuffer.putDouble(i2, read3);
                        int i23 = i2 + 8;
                        byteBuffer.putDouble(i23, read4);
                        i2 = i23 + 8;
                        if (z) {
                            if (attributeStreamOfDbl3 != null) {
                                defaultValue2 = attributeStreamOfDbl3.read(i20);
                            } else {
                                defaultValue2 = VertexDescription.getDefaultValue(1);
                            }
                            byteBuffer.putDouble(i2, defaultValue2);
                            i2 += 8;
                        }
                        if (z2) {
                            if (attributeStreamOfDbl2 != null) {
                                defaultValue = attributeStreamOfDbl2.read(i20);
                            } else {
                                defaultValue = VertexDescription.getDefaultValue(2);
                            }
                            byteBuffer.putDouble(i2, defaultValue);
                            i2 += 8;
                        }
                        i20--;
                        i9 = i22;
                    }
                    i8 = i18 + 1;
                    b = b2;
                    i9 = pathEnd;
                    pathCount = i16;
                    multiPathImpl = multiPathImpl2;
                    i7 = i17;
                }
                i3++;
                b = b;
                i8 = i13;
                pathCount = pathCount;
                a = i12;
            }
            return i2;
        }
        throw new GeometryException("invalid call");
    }
}
